<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/10/13
 * Time: 17:53
 */

namespace app\admin\model;


use think\Model;

class Goods extends Model
{
    // 将图相对地址片转为网络地址
    public function getOriginalImgAttr($value)
    {
        return "http://img.huaxiaoujia.com/" . $value;
    }

    public function getAddressAttr($value)
    {
        $address = db("business")->field("ShortName")->alias("b")->join("k_region r", "b.zipcode = r.ZipCode")->find($value)["ShortName"];
        if (!$address) {
            $address = "凯里市";
        }
        return $address;
    }

    public function getPriceAttr($value)
    {
        return db("spec_goods_price")->field("price")->where("goods_id", $value)->order("price")->find()["price"];
    }

    public function getPriceListAttr($value)
    {
        $priceList = db("spec_goods_price")->field("id,price,company as unit,img,spec_name as specName")->where("goods_id", $value)->order("sort")->select();
        foreach ($priceList as $k => $v) {
            if ($v["img"]) {
                $priceList[$k]["img"] = "http://img.huaxiaoujia.com/" . $v["img"];
            }
        }
        return $priceList;
    }

    public function getContentAttr($value)
    {
        $html = htmlspecialchars_decode($value);
        //取src
        preg_match_all('/ src="(.+?)"/', $html, $match);
        $img = [];
        foreach ($match[1] as $k => $v) {
            if (substr($v, 0, 4) != "http") {
                $v = "http://huaxiaoujia.com" . $v;
            }
            $img[] = $v;
        }
        return $img;
    }
}